' AdverseEvent Validation Script
' 7/9/07 2:00 pm

Option Strict Off
imports System
imports System.Text.RegularExpressions
imports Humans=PHSRAG.Insight21.Humans
imports PHSRAG.Insight21.BusinessObjects
imports FormsManagement = PHSRAG.Insight21.FormsManagement
imports System.Collections
imports Utility=PHSRAG.Utility

module Script
	sub Main()
    
       	Dim questionnaireAnswers As ArrayList = intrinsicProtocolVersion.QuestionnaireAnswers
       	Dim qV As FormsManagement.QuestionnaireVersion
       	Dim questionnaireAnswer As FormsManagement.QuestionnaireAnswer
       	dim answers as ArrayList = Nothing
		dim answer as FormsManagement.Answer
		const DATEPATTERN as string= "(0[1-9]|1[012])[/](0[1-9]|[12][0-9]|3[01])[/]\d\d\d\d"
		' Pages for rediction
		const AttachmentsPageRedirection = -1

		'verify if all the ProtocolDocument records are either marked as hardcopy or there is an attachment tied to it
		For Each pD As Humans.ProtocolDocument In intrinsicProtocolProcess.ProtocolDocuments
			If not (pD.IsHardCopy or pD.DocumentId >0) Then
				LogMissingItem(AttachmentsPageRedirection, AttachmentsPageRedirection, _
						"Attachments", string.empty, string.empty, string.empty, "Attachment missing for " + pD.Name)
			End if  
		Next
		
        For Each questionnaireAnswer In questionnaireAnswers
			if (questionnaireAnswer.QuestionnaireCategory = "QUESTCAT_AE_IQ") then
				answers = questionnaireAnswer.GetAnswers("EventDate")
				If not (answers Is Nothing) then
					if (answers.Count > 0) then
						if not IsDate(answers(0).Value) or not Regex.IsMatch(answers(0).Value, DATEPATTERN) then
							LogMissingItem(questionnaireAnswer.QuestionnaireVersionId, questionnaireAnswer.Id, questionnaireAnswer.Name, questionnaireAnswer.QuestionnaireVersion.Questionnaire.Name, questionnaireAnswer.QuestionnaireCategory, "Adverse Event", _
								"You must enter the Event Date in mm/dd/yyyy format.")
						end if
					end if
				end if
			
				answers = questionnaireAnswer.GetAnswers("EventNotifiedDate")
				If not (answers Is Nothing) then
					if (answers.Count > 0) then
						if not IsDate(answers(0).Value) or not Regex.IsMatch(answers(0).Value, DATEPATTERN) then
							LogMissingItem(questionnaireAnswer.QuestionnaireVersionId, questionnaireAnswer.Id, questionnaireAnswer.Name, questionnaireAnswer.QuestionnaireVersion.Questionnaire.Name, questionnaireAnswer.QuestionnaireCategory, "Adverse Event", _
								"You must enter the Event Notified Date in mm/dd/yyyy format.")
						end if
					end if
				end if
				
				Exit For
			
			end if
		Next	
    End Sub
    
    'Save LogItem
    Sub SaveLogItem(ByVal logItem As Humans.LogItem)
		intrinsicProtocolProcess.SaveLogItem(logItem)
    End Sub
    
    private function LogMissingItem(byval questionnaireVersionId as integer, byval questionnaireAnswerId as integer, byval logType as string, byval category as string, byval questionName as string, byval answer as string, byval description as string)
		Dim logItem As New Humans.LogItem(intrinsicCachePolicy)
		logItem.QuestionnaireVersionId = questionnaireVersionId
		logItem.QuestionnaireAnswerId = questionnaireAnswerId
		logItem.Type = logType
		logItem.QuestionName = questionName
		logItem.Category = category
		logItem.Answer = answer
		logItem.Description = description
		logItem.Completed =False
		SaveLogItem(logItem)
	end function
									
end module